window.onload=function (){
    var info= getQuery("errorInfo");
    if (info=="noland"){
        $("errorDiv").innerHTML="对不起，请先登录"
    }
    findAll()
}
function findAll(){
    axios.get("/user/findAll").then(resp=>{
        var info = resp.data;
        str ="";
        info.forEach(n=>{
            str+=`<tr><td>${n.userName}</td><td>${n.pwd}</td><td>${n.realName}</td><td>${n.grade}</td><td><input type="button" value="删除用户" onclick="delUser(${n.userId})"><input type="button" value="修改用户" onclick="changeUser(${n.userId})"></td></tr>`
        })
        $("data").innerHTML=str;
    })
}

function delUser(userId){
    axios.get("/user/del",{
        params:{
            userId
        }
    }).then(resp=>{
        var info=resp.data;
        if (info=="ok"){
            findAll()
        }else {
            alert("不能删除自己")
        }
    })
}

function changeUser(userId){
    showUpdateDiv()
    axios.get("/user/findById",{
        params: {
            userId
        }
    }).then(resp=>{
        var info =resp.data;
        $("id").value=info.userId;
        $("userNameSpan").innerHTML=info.userName;
        $("nameSpan").innerHTML=info.realName;
    })
}

function changeUsers(){
    axios.get("/user/updateGrade",{
        params:{
            userId:$("id").value,
            grade:$("userSelect").value
        }
    }).then(resp=>{
        var info = resp.data;
        if (info=="ok"){
            exit()
            findAll()
        }
    })
}
function showAddDiv(){
    $("addDiv").style.visibility="visible";
}
function add(){
    axios.get("/user/add",{
        params:{
            userName:$("userName").value,
            realName:$("name").value,
            grade:$("gradeSelect").value
        }
    }).then(resp=>{
        var info =resp.data;

        if (info=="ok"){
            exit()
            findAll()
        }else {
            $("errorSpan").innerHTML="用户已存在"
            $("errorSpan").style.color="red"
        }
    })
}
function showUpdateDiv(){
    $("updateDiv").style.visibility="visible";
}

function exit(){
    $("addDiv").style.visibility="hidden";
    $("updateDiv").style.visibility="hidden";
}